package com.yunxi.job;

import com.alibaba.fastjson.JSONObject;
import com.yunxi.core.base.Constant;
import com.yunxi.core.util.BeanTools;
import com.yunxi.core.util.HttpUtil;
import com.yunxi.model.eai.EaiLog;
import com.yunxi.service.eai.EaiLogService;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2019/6/4.
 */
@DisallowConcurrentExecution
@Component
public class PendPlanJob implements BaseJob {
    private static Logger _log = LoggerFactory.getLogger(PendPlanJob.class);

    @Autowired
    @Qualifier("eaiLogServiceImpl")
    EaiLogService eaiLogService;
    @Value("${SAP.URL}")
    private String SAP_URL;
    public PendPlanJob() {
    }
    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        Map map=new HashMap();
        map.put("type",1);
        map.put("disposeStatus",0);
        //查询失败次数小于5次的异步接口数据
        List<EaiLog> list = eaiLogService.eaiLoglistByType(map);
        for (int i = 0; i < list.size(); i++) {
            EaiLog eaiLog =  list.get(i);
            sendPlan(eaiLog);
        }
        System.out.println(list);
    }

    private void sendPlan(EaiLog eaiLog) {
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        try {
            eaiLog.setRequestSendTime(new Date());
            Map map = HttpUtil.sendPost(eaiLog.getPmsEaiContent(),eaiLog.getServiceName(), eaiLog.getSourceSystem(), SAP_URL);
            eaiLog.setRequestId(String.valueOf(map.get("requestId")));
            eaiLog.setResponseReceiveTime(new Date());
            eaiLog.setEsbResultContent(JSONObject.toJSONString(map));
            Object object = map.get("header");
            Map<String, Object> headerMap = BeanTools.objectToMap(object);
            String statusFlag=String.valueOf(headerMap.get("statusFlag"));
            String response = String.valueOf(map.get("response"));
            if (statusFlag.equals("0")){//这里只对ESB的处理状态做判断
                //处理失败，更状态位失败
                eaiLog.setDisposeStatus(2);
            }else {
                JSONObject jsonResponse = JSONObject.parseObject(response);
                if (String.valueOf(jsonResponse.get("code")).equals("E")){
                    //处理失败，更状态位失败
                    eaiLog.setDisposeStatus(2);
                    eaiLog.setDisposeCount(eaiLog.getDisposeCount()+1);
                }else {
                    eaiLog.setDisposeStatus(1);
                }
            }
        } catch (Exception e) {
            _log.error(e.getMessage(),e);
            eaiLog.setDisposeCount(eaiLog.getDisposeCount()+1);
            eaiLog.setDisposeStatus(2);
        }
        eaiLog.setUpdateDate(new Date());
        eaiLogService.saveOrUpdate(eaiLog);
    }


}
